package org.chen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.chen.domain.entity.Category;

import java.util.List;

@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
    @Select("SELECT * FROM sys_category WHERE parent_id = #{parentId} ORDER BY sort")
    List<Category> selectByParentId(@Param("parentId") String parentId);

    @Update("UPDATE sys_category SET path = #{newPath} WHERE id = #{id}")
    int updatePath(@Param("id") String id, @Param("newPath") String newPath);

    @Select({
            "SELECT COUNT(*) FROM sys_category",
            "WHERE parent_id = #{parentId}",
            "AND name = #{name}",
            "AND id != #{excludeId}"
    })
    int countSameNameSiblings(@Param("parentId") String parentId,
                              @Param("name") String name,
                              @Param("excludeId") String excludeId);
}
